import pyautogui
import time

#pyautogui库其他用法 https://blog.csdn.net/qingfengxd1/article/details/108270159
'''
定义鼠标事件
代码重构:2021.11.25 11:50 AM
原有问题:三个if分支都有较多的重复语句
        1次 多次 无数次 三种分支过于冗余
改进:将重复语句提取
    将分支简化为 n次和无数次
'''
def mouseClicked(clickTimes,lOrR,img,reTry):
    if reTry == 1:
        while True:
            location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
                break
            print("未找到匹配图片,0.1秒后重试")
            time.sleep(0.1)
    elif reTry == -1:
        while True:
            location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
            time.sleep(0.1)
    elif reTry > 1:
        i = 1
        while i < reTry + 1:
            location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
                print("重复")
                i += 1
            time.sleep(0.1)

def mouseClick(clickTimes,lOrR,img,reTry):
    i = 0
    while i < reTry or reTry==-1:
        location = pyautogui.locateCenterOnScreen(img, confidence=0.9)
        if location is not None:
            pyautogui.click(location.x, location.y, clicks=clickTimes, interval=0.2, duration=0.2, button=lOrR)
            i += 1
            print("此步骤已是实现" + str(i) + "次")
        print("未找到匹配图片,0.1秒后重试")
        time.sleep(0.1)